package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.Withdraw;
import co.yixiang.modules.system.service.WithdrawService;
import co.yixiang.modules.system.dto.queryCriteria.WithdrawQueryCriteria;
import co.yixiang.modules.system.dto.WithdrawDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author db
* @date 2020-11-16
*/
@AllArgsConstructor
@Api(tags = "提现记录管理")
@RestController
@RequestMapping("/api/withdraw")
public class WithdrawController {

    private final WithdrawService withdrawService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','withdraw:list')")
    public void download(HttpServletResponse response, WithdrawQueryCriteria criteria) throws IOException {
        withdrawService.download(Convert.toList(WithdrawDto.class, withdrawService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询提现记录")
    @ApiOperation("查询提现记录")
    @PreAuthorize("@el.check('admin','withdraw:list')")
    public ResponseEntity<Object> getWithdraws(WithdrawQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(withdrawService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增提现记录")
    @ApiOperation("新增提现记录")
    @PreAuthorize("@el.check('admin','withdraw:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Withdraw resources){
        return new ResponseEntity<>(withdrawService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改提现记录")
    @ApiOperation("修改提现记录")
    @PreAuthorize("@el.check('admin','withdraw:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Withdraw resources){
        withdrawService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除提现记录")
    @ApiOperation("删除提现记录")
    @PreAuthorize("@el.check('admin','withdraw:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        withdrawService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}